package io.renren.modules.generator.service.impl;

import io.renren.constant.DeviceFinal;
import io.renren.modules.generator.dao.DeviceSiteDao;
import io.renren.modules.generator.dto.DeviceWarningQuotaMainDto;
import io.renren.modules.generator.dto.DeviceWarningQuotaSubDto;
import io.renren.modules.generator.entity.DeviceSiteEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.generator.dao.DeviceHourDao;
import io.renren.modules.generator.entity.DeviceHourEntity;
import io.renren.modules.generator.service.DeviceHourService;

@Slf4j
@Service("deviceHourService")
public class DeviceHourServiceImpl extends ServiceImpl<DeviceHourDao, DeviceHourEntity> implements DeviceHourService {

    @Autowired
    private DeviceHourDao deviceHourDao;
    @Autowired
    private DeviceSiteDao deviceSiteDao;

    @Override
    public List<DeviceHourEntity> selectDeviceHourEntityListByParams(Map<String, Object> params) {
        QueryWrapper<DeviceHourEntity> wrapper = new QueryWrapper<>();
        if(Objects.nonNull(params.get("deviceType"))){
            QueryWrapper<DeviceSiteEntity> deviceSiteWrapper = new QueryWrapper<>();
            deviceSiteWrapper.eq("device_type",params.get("deviceType"));
            List<DeviceSiteEntity> deviceSiteEntityList = deviceSiteDao.selectList(deviceSiteWrapper);
            List<String> deviceEncryptList = new ArrayList<>();
            if(Objects.nonNull(deviceSiteEntityList)){
                deviceSiteEntityList.forEach(deviceSiteEntity -> {
                    deviceEncryptList.add(deviceSiteEntity.getDeviceCode());
                });
                wrapper.in("device_encrypt",deviceEncryptList);
            }
        }
        if(Objects.nonNull(params.get("deviceEncrypt"))){
            wrapper.eq("device_encrypt",params.get("deviceEncrypt"));
        }
        if(Objects.nonNull(params.get("deviceCode"))){
            wrapper.eq("device_code",params.get("deviceCode"));
        }
        if(Objects.nonNull(params.get("startTime")) && Objects.nonNull(params.get("endTime"))){
            wrapper.between("create_time",params.get("startTime"),params.get("endTime"));
        }
        wrapper.orderByDesc("create_time");
        return deviceHourDao.selectList(wrapper);
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<DeviceHourEntity> wrapper = new QueryWrapper<>();
        if(Objects.nonNull(params.get("deviceType"))){
            QueryWrapper<DeviceSiteEntity> deviceSiteWrapper = new QueryWrapper<>();
            deviceSiteWrapper.eq("device_type",params.get("deviceType"));
            List<DeviceSiteEntity> deviceSiteEntityList = deviceSiteDao.selectList(deviceSiteWrapper);
            List<String> deviceEncryptList = new ArrayList<>();
            if(Objects.nonNull(deviceSiteEntityList)){
                deviceSiteEntityList.forEach(deviceSiteEntity -> {
                    deviceEncryptList.add(deviceSiteEntity.getDeviceCode());
                });
                wrapper.in("device_encrypt",deviceEncryptList);
            }
        }
        if(Objects.nonNull(params.get("deviceEncrypt"))){
            wrapper.eq("device_encrypt",params.get("deviceEncrypt"));
        }
        if(Objects.nonNull(params.get("deviceCode"))){
            wrapper.eq("device_code",params.get("deviceCode"));
        }
        if(Objects.nonNull(params.get("startTime")) && Objects.nonNull(params.get("endTime"))){
            wrapper.between("create_time",params.get("startTime"),params.get("endTime"));
        }
        wrapper.orderByDesc("create_time");
        IPage<DeviceHourEntity> page = this.page(
                new Query<DeviceHourEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public String selectAvgQuota(Map<String, String> queryMap) {
        return deviceHourDao.selectAvgQuota(queryMap);
    }

    @Override
    public Object quota(String[] deviceCodes, String time) {
        Map<String, DeviceWarningQuotaMainDto> resultMap = new HashMap<>();
        log.info("deviceCodes:"+deviceCodes);
        DeviceWarningQuotaMainDto deviceWarningQuotaMainDto;
        DeviceWarningQuotaSubDto deviceWarningQuotaSubDto;
        List<DeviceWarningQuotaSubDto> deviceSubQuotaList;
        QueryWrapper<DeviceHourEntity> queryWrapper;
        List<DeviceHourEntity> deviceHourEntityList;
        QueryWrapper<DeviceSiteEntity> deviceSiteEntityQueryWrapper;
        DeviceSiteEntity deviceSiteEntity;
        try{
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
            Date date = df.parse(time);
            for (int i = 0; i < deviceCodes.length; i++) {
                deviceSiteEntityQueryWrapper = new QueryWrapper<>();
                deviceSiteEntityQueryWrapper.lambda().eq(DeviceSiteEntity::getDeviceCode,deviceCodes[i]);
                deviceSiteEntity = deviceSiteDao.selectOne(deviceSiteEntityQueryWrapper);
                deviceSubQuotaList = new ArrayList<>();
                queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda()
                        .eq(DeviceHourEntity::getDeviceEncrypt,deviceCodes[i])
                        .eq(DeviceHourEntity::getCreateTime,date);
                deviceHourEntityList = deviceHourDao.selectList(queryWrapper);
                for (int j = 0; j < deviceHourEntityList.size(); j++) {
                    deviceWarningQuotaSubDto = DeviceWarningQuotaSubDto.builder()
                            .deviceCode(deviceHourEntityList.get(j).getDeviceCode())
                            .deviceCodeName(DeviceFinal.allQuotaMap.get(deviceHourEntityList.get(j).getDeviceCode()))
                            .actualQuota(deviceHourEntityList.get(j).getDeviceValue())
                            .build();
                    deviceSubQuotaList.add(deviceWarningQuotaSubDto);
                }
                deviceWarningQuotaMainDto = DeviceWarningQuotaMainDto.builder()
                        .deviceWarningQuotaSubDtoList(deviceSubQuotaList)
                        .deviceEncrypt(deviceCodes[i])
                        .deviceSiteName(deviceSiteEntity.getSiteName())
                        .build();
                resultMap.put(deviceCodes[i], deviceWarningQuotaMainDto);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }


}